<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/4/25
 * Time: 10:00
 */
namespace app\admin\controller;
use think\Db;
class Advert extends Base {
    public function index(){
        if(request()->isAjax()){
            $where = array();
            $params = input('param.');
            if(isset($params['lunbo_status']) && $params['lunbo_status'] != '')
            {
                $where['a.lunbo_status'] = $params['lunbo_status'];
            }
            if(isset($params['lunbo_type']) && $params['lunbo_type'] != '')
            {
                $where['a.lunbo_type'] = $params['lunbo_type'];
            }
//            if(isset($params['key']) && $params['key'] != '')
//            {
//                $where['coupon_name'] = ['like', "%" . $params['key'] . "%"];
//            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('advert_video')->alias('a')
                ->where($where)->count();// 获取总条数
            $lists = Db::name('advert_video')->alias('a')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('a.lunbo_px asc,a.lunbo_status asc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    //更新排序
    public function edit(){
        extract(input());
        Db::startTrans();// 启动事务
        try{
           Db::table('up_advert_video')->where(['lunbo_id' => $id ])->setField($field , $value);
            Db::commit();// 提交事务
            return ['code' => 200,'data' => '', 'msg' => '排序更新成功'];
        }catch( \Exception $e){
            Db::rollback ();//回滚事务

            return ['code' => 100, 'data' => '', 'msg' => '排序更新失败'];
        }
    }

    //启用
    public function enable(){
        $id = input('param.id');
        extract(input());
        Db::startTrans();// 启动事务
        try{
            Db::table('up_advert_video')->where(['lunbo_id' => $id ])->setField('lunbo_status' , 1);
            Db::commit();// 提交事务
            return ['code' => 200,'data' => '', 'msg' => '操作成功'];
        }catch( \Exception $e){
            Db::rollback ();//回滚事务

            return ['code' => 100, 'data' => '', 'msg' => '操作失败'];
        }
    }
    //禁用
    public function prohibit(){
        $id = input('param.id');
        Db::startTrans();// 启动事务
        try{
            Db::table('up_advert_video')->where(['lunbo_id' => $id ])->setField('lunbo_status' , 2);
            Db::commit();// 提交事务
            return ['code' => 200,'data' => '', 'msg' => '操作成功'];
        }catch( \Exception $e){
            Db::rollback ();//回滚事务

            return ['code' => 100, 'data' => '', 'msg' => '操作失败'];
        }
    }
    //删除
    public function del(){
        $id = input('param.id');
        Db::startTrans();// 启动事务
        try{
            Db::table('up_advert_video')->where(['lunbo_id' => $id ])->delete();
            Db::commit();// 提交事务
            return ['code' => 200,'data' => '', 'msg' => '操作成功'];
        }catch( \Exception $e){
            Db::rollback ();//回滚事务

            return ['code' => 100, 'data' => '', 'msg' => '操作失败'];
        }
    }

    //添加
    public function add(){
        if(request()->isPost()){
            $lunbo_name = input('param.lunbo_name');
            $lunbo_type = input('param.lunbo_type');
            $lunbo_image = input('param.lunbo_image');
            $lunbo_status = input('param.lunbo_status');
            $lunbo_px = input('lunbo_px');
            $data['lunbo_name'] = $lunbo_name;
            $data['lunbo_type'] = $lunbo_type;
            $data['lunbo_image'] = $lunbo_image;
            $data['lunbo_status'] = $lunbo_status;
            $data['lunbo_create_time'] = date('Y-m-d H:i:s');
            $data['lunbo_update_time'] = date('Y-m-d H:i:s');
            $data['lunbo_px'] = $lunbo_px;
            $res = Db::table('up_advert_video')->insertGetId($data);
            if($res){
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }
    //编辑
    public function update(){
        if(request()->isPost()){
            $id = input('param.id');
            $lunbo_name = input('param.lunbo_name');
            $lunbo_status = input('param.lunbo_status');
            $lunbo_px = input('lunbo_px');
            if(!empty(input('lunbo_image'))){
                $data['lunbo_image'] = input('lunbo_image');
            }
            $data['lunbo_name'] = $lunbo_name;
            $data['lunbo_status'] = $lunbo_status;
            $data['lunbo_update_time'] = date('Y-m-d H:i:s');
            $data['lunbo_px'] = $lunbo_px;
            $res = Db::table('up_advert_video')->where('lunbo_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'data' =>"", 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '编辑失败']);
            }
        }
        $id = input('param.id');
        $res = Db::table('up_advert_video')->where('lunbo_id',$id)->find();
        $this->assign('res',$res);
        return $this->fetch();
    }

}